package com.hgkj.cloud.base.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理常量类
 */
public class HcStringUtils {

    private HcStringUtils(){
    }

    public static String parseString(Object o) {
        return parseString(o, "");
    }

    public static String parseString(Object o, String defValue) {
        return o == null ? defValue : o.toString();
    }

    public static int parseInt(Object o, int defValue) {
        if (o == null) {
            return defValue;
        }
        try {
            return Integer.parseInt(o.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    public static int parseInt(Object o) {
        return parseInt(o, 0);
    }

    public static long parseLong(Object o, Long defValue) {
        if (o == null) {
            return defValue;
        }
        try {
            return Long.parseLong(o.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    public static long parseLong(Object o, int defValue) {
        if (o == null) {
            return defValue;
        }
        try {
            return Long.parseLong(o.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    public static long parseLong(Object o) {
        return parseLong(o, 0l);
    }

    public static boolean isMobile(String mobiles){
        Pattern p = Pattern.compile("^1\\d{10}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * @param string
     * @return 转换之后的内容
     * @Title: unicodeDecode
     * @Description: unicode解码 将Unicode的编码转换为中文
     */
    public static String unicodeDecode(String string) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(string);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            string = string.replace(matcher.group(1), ch + "");
        }
        return string;
    }

    public static int countCharInString(String str, Object searchChar) {
        int thisLength = str.length();
        str = str.replaceAll(searchChar.toString(), "");
        int newLength = str.length();
        return thisLength - newLength;
    }
}
